package com.ego.order.listener;

import com.ego.common.to.order.OrderTo;
import com.ego.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: ego
 * @description: 订单关闭监听器
 * @author: KamTang
 * @create: 2022-12-26 09:10
 **/
@RabbitListener(queues = "order.release.queue")
@Component
@Slf4j
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void close(OrderTo orderTo, Message message, Channel channel) throws IOException {
        try {
            log.info("开始关闭订单。。。");
            orderService.close(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
